﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WmsApp.Lookup;
using WmsApp.Security;
using WmsApp.Service;
using WmsApp.WmsService;

namespace WmsApp.UI
{
    public partial class PayApplyBillItemCtl : DevExpress.XtraEditors.XtraUserControl
    {
        private static object _payApplyBillItemCtlCmdExcutingObject = new object(); 
        private PayApplyBillItem payApplyBillItem = null;
        public PayApplyBillItemCtl()
        {
            InitializeComponent();
            if (GlobalClass.RunTimeMode)
            {
                LookupBind.BindingLookUpEditSystemUser(this.lookUpEditPayID, this.textEditPayName, "PayName");

                payApplyBillItem = new PayApplyBillItem
                                       {
                                           PayTime = DateTime.Now.Date,
                                           ItemState = "正常",
                                           Idx = 1,
                                           PayID = UserSession.CurrentUser.USERID,
                                           PayName =  UserSession.CurrentUser.USERNAME,
                                           PayAmount = 0,
                                       };
            }
        }
        
        public event PayApplyBillItemCtlCmdExcutingEventHandler PayApplyBillItemCtlCmdExcuting
        {
            add
            {
                Events.AddHandler(_payApplyBillItemCtlCmdExcutingObject, value);
            }
            remove
            {
                Events.RemoveHandler(_payApplyBillItemCtlCmdExcutingObject, value);
            }
        }

        public void BindingPayApplyBillItem(PayApplyBillItem item)
        {
            this.payApplyBillItem = item;
            BindingControls();
            EnableControls(false);
        }

        public void BindingPayApplyBillID(string payApplyBillID)
        {
            payApplyBillItem.PayApplyBillID = payApplyBillID;
            BindingControls();
            EnableControls(true);
        }

        public string PayApplyBillItemID { get; private set; }

        private void EnableControls(bool b)
        {
            this.CmdSave.Enabled = b;
            this.CmdCancel.Enabled = b;

            this.dateEditPayTime.Enabled = b;
            this.calcEditPayAmount.Enabled = b;
            this.lookUpEditPayID.Enabled = b;
            this.memoEditPayRemark.Enabled = b;
        }

        private void BindingControls()
        {
            this.textEditBillID.DataBindings.Clear();
            this.textEditBillID.DataBindings.Add("EditValue", this.payApplyBillItem, "PayApplyBillID");

            this.textEditIdx.DataBindings.Clear();
            this.textEditIdx.DataBindings.Add("EditValue", this.payApplyBillItem, "Idx");

            this.dateEditPayTime.DataBindings.Clear();
            this.dateEditPayTime.DataBindings.Add("EditValue", this.payApplyBillItem, "PayTime",true,DataSourceUpdateMode.OnPropertyChanged);

            this.textEditItemState.DataBindings.Clear();
            this.textEditItemState.DataBindings.Add("EditValue", this.payApplyBillItem, "ItemState");

            this.calcEditPayAmount.DataBindings.Clear();
            this.calcEditPayAmount.DataBindings.Add("EditValue", this.payApplyBillItem, "PayAmount", true, DataSourceUpdateMode.OnPropertyChanged);

            this.dateEditCancelItemDate.DataBindings.Clear();
            this.dateEditCancelItemDate.DataBindings.Add("EditValue", this.payApplyBillItem, "CancelItemDate");

            this.lookUpEditPayID.DataBindings.Clear();
            this.lookUpEditPayID.DataBindings.Add("EditValue", this.payApplyBillItem, "PayID", true, DataSourceUpdateMode.OnPropertyChanged);

            this.textEditPayName.DataBindings.Clear();
            this.textEditPayName.DataBindings.Add("EditValue", this.payApplyBillItem, "PayName");

            this.textEditCancelID.DataBindings.Clear();
            this.textEditCancelID.DataBindings.Add("EditValue", this.payApplyBillItem, "CancelID");

            this.textEditCancelName.DataBindings.Clear();
            this.textEditCancelName.DataBindings.Add("EditValue", this.payApplyBillItem, "CancelName");

            this.memoEditPayRemark.DataBindings.Clear();
            this.memoEditPayRemark.DataBindings.Add("EditValue", this.payApplyBillItem, "PayRemark");
        }

        private void CmdSave_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(payApplyBillItem.PayApplyBillID))
            {
                XtraMessageBox.Show("没有付款申请单号码！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (String.IsNullOrEmpty(payApplyBillItem.PayID))
            {
                XtraMessageBox.Show("请输入付款人！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if ((payApplyBillItem.PayAmount??0) == 0)
            {
                XtraMessageBox.Show("付款金额为0！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            try
            {
                //var service = EntityServiceFactory.GetEntityService<PayApplyBillItem, PayApplyBillItemService>();
                var client = Factory.WmsServiceFactory.GetServiceClient();
                PayApplyBillItemID = client.NewPayApplyBillItem(payApplyBillItem);
                if (Events[_payApplyBillItemCtlCmdExcutingObject] != null)
                {
                    var @delegate = (PayApplyBillItemCtlCmdExcutingEventHandler)Events[_payApplyBillItemCtlCmdExcutingObject];
                    @delegate(PayApplyBillItemCmdExcutingType.Saving, PayApplyBillItemID);
                }
            }
            catch (Exception ex)
            {
                Exceptions.ExceptionHandler.ShowException(ex);
            }
        }

        private void CmdCancel_Click(object sender, EventArgs e)
        {
            if (Events[_payApplyBillItemCtlCmdExcutingObject] != null)
            {
                var @delegate = (PayApplyBillItemCtlCmdExcutingEventHandler)Events[_payApplyBillItemCtlCmdExcutingObject];
                @delegate(PayApplyBillItemCmdExcutingType.Closing, String.Empty);
            }
        }
    }

    /// <summary>
    /// 付款单编辑界面点击确定或者取消所要激发事件的委托定义
    /// </summary>
    /// <param name="payApplyBillItemCmdExcutingType">点击按钮类型，保存或者关闭</param>
    /// <param name="payApplyBillItemID">当前付款Item的编号Guid</param>
    public delegate void PayApplyBillItemCtlCmdExcutingEventHandler(PayApplyBillItemCmdExcutingType payApplyBillItemCmdExcutingType, string payApplyBillItemID);

    /// <summary>
    /// 点击按钮类型
    /// </summary>
    public enum PayApplyBillItemCmdExcutingType
    {
        Closing = 0,
        Saving = 1
    }
}
